package controllers

import (
	"baokaobang/datasourse"
	"baokaobang/models"
	"baokaobang/utils"
	"fmt"
	"github.com/kataras/iris/v12"
	"time"
)

type Enrollment struct {
	Ctx iris.Context
}

// 获取某大学录取数据
func (c *Enrollment) PostList() interface{} {
	id := c.Ctx.PostValue("id")

	if len(id) <= 0 {
		return utils.CommonRes(1000, "请提供大学id", "")
	}

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库：%s", err.Error()), "")
	}

	list := make([]models.Enrollment, 0)

	db.Table(new(models.Enrollment)).Where("university_id = ?", id).
		Distinct("college_id", "batch_subject").Find(&list)

	year := utils.Atoi(time.Now().Format("2006"))

	enrollmentList := make([]models.EnrollmentData, 0)
	for _, item := range list {
		enrollmentData := models.EnrollmentData{}
		enrollmentData.BatchSubject = item.BatchSubject

		college := new(models.College)
		db.ID(item.CollegeId).Get(college)
		enrollmentData.CollegeName = college.Name

		enrollmentOne := new(models.Enrollment)
		db.Where("university_id = ? and college_id = ? and year = ? and batch_subject = ?", id, item.CollegeId, year-1, item.BatchSubject).Get(enrollmentOne)

		enrollmentTwo := new(models.Enrollment)
		db.Where("university_id = ? and college_id = ? and year = ? and batch_subject = ?", id, item.CollegeId, year-2, item.BatchSubject).Get(enrollmentTwo)

		enrollmentThree := new(models.Enrollment)
		db.Where("university_id = ? and college_id = ? and year = ? and batch_subject = ?", id, item.CollegeId, year-3, item.BatchSubject).Get(enrollmentThree)

		enrollmentData.DataList = [][]string{
			{"计划人数", enrollmentOne.PlanNum, enrollmentTwo.PlanNum, enrollmentThree.PlanNum},
			{"实际人数", enrollmentOne.ActualNum, enrollmentTwo.ActualNum, enrollmentThree.ActualNum},
			{"录取最高分", enrollmentOne.HighScore, enrollmentTwo.HighScore, enrollmentThree.HighScore},
			{"最高分位次", enrollmentOne.HighScoreRank, enrollmentTwo.HighScoreRank, enrollmentThree.HighScoreRank},
			{"录取最低分", enrollmentOne.LowScore, enrollmentTwo.LowScore, enrollmentThree.LowScore},
			{"最低分位次", enrollmentOne.LowScoreRank, enrollmentTwo.LowScoreRank, enrollmentThree.LowScoreRank},
			{"录取批次线", enrollmentOne.BatchLine, enrollmentTwo.BatchLine, enrollmentThree.BatchLine},
		}

		enrollmentList = append(enrollmentList, enrollmentData)
	}

	data := map[string]interface{}{
		"years": []string{"", fmt.Sprintf("%d", year-1), fmt.Sprintf("%d", year-2), fmt.Sprintf("%d", year-3)},
		"list": enrollmentList,
	}

	return utils.CommonRes(20000, "操作成功", data)
}
